package com.jml.account.sysUser.controller;


import com.jml.account.activemq.service.MqSendMessageService;
import com.jml.account.sysMenu.service.SysMenuService;
import com.jml.account.sysUser.entity.SysUser;
import com.jml.account.system.exception.LoginException;
import com.jml.account.test.service.TestService;
import javax.servlet.http.HttpServletRequest;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class ShiroController {





    @RequestMapping("/")
    private String  getItemById(HttpServletRequest request) {
        return "/meau_jsp/login";

    }





    //登录提交地址应该要和shiro中loginUrl配置的地址一致
    @RequestMapping(value = "login",method = RequestMethod.POST)
    public String login(HttpServletRequest request)throws Exception{

        //如果登陆失败从request中获取认证异常信息，shiroLoginFailure就是shiro异常类的全限定名
        String exceptionClassName = (String) request.getAttribute("shiroLoginFailure");
        //根据shiro返回的异常类路径判断，抛出指定异常信息
        if(exceptionClassName!=null){
            String msg="";
            if (UnknownAccountException.class.getName().equals(exceptionClassName)) {
                msg="账号不存在";
            } else if (IncorrectCredentialsException.class.getName().equals(exceptionClassName)) {
                msg="用户名/密码错误";
            } else if("randomCodeError".equals(exceptionClassName)){
                msg="验证码错误 ";
            }else {
                msg="未知错误 ";//最终在异常处理器生成未知错误
            }
            throw new LoginException(msg);
        }
        //此方法不处理登陆成功（认证成功），shiro认证成功会自动跳转到上一个请求路径
        //登陆失败还到login页面
        return "meau_jsp/login";
    }

    @RequestMapping(value = "login",method = RequestMethod.GET)
    public String login1(HttpServletRequest request)throws Exception{
        return "meau_jsp/login";
    }


    //默认的认证成功登录页面
    @RequestMapping("first")
    private String  first(HttpServletRequest request,Model model) {
        //去除shiro中取身份信息
        Subject subject = SecurityUtils.getSubject();
        SysUser sysUser = (SysUser) subject.getPrincipal();
        //通过model传到页面
        model.addAttribute("activeUser", sysUser);
        return "sysUser/userSetting";
    }


    @RequestMapping("first1")
    private String  first1(HttpServletRequest request) {
        System.out.println("qqqqqqqqq");
        return "meau_jsp/meau_index";
    }

    //权限不足
    @RequestMapping("refuse")
    private String  refuse(HttpServletRequest request) {
        System.out.println("refuse");
        return "meau_jsp/test_refuse";
    }


    @RequestMapping("/abc/queryItems")
    @RequiresPermissions("item:edit")
    public String queryItems1(HttpServletRequest request) {
        System.out.println("-----------------------------------");
        return "meau_jsp/meau_index";
    }



    @RequestMapping("/item/queryItems")
    @RequiresPermissions("item:query")
    public String item(HttpServletRequest request) {
        System.out.println("-----------------------------------");
        return "meau_jsp/meau_index";
    }
}
